package com.mhuang.module.job.service;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mhuang.module.job.JobService;
import com.mhuang.quartz.model.SchedulerJob;
import com.mhuang.wechat.common.utils.BeanUtils;

@Service("jobService")
public class JobServiceImpl implements JobService {
	
    private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private JdbcTemplate jdbcTemplate; 
	
	@Override
	public void executeTask(long taskId) {
		
		Map<String,Object> crowd=null;
		try {

			//=========开始启动抽奖程序===========
			
			//=========结束抽奖程序===========
 		} catch (EmptyResultDataAccessException e) {
			logger.debug("当前还未开启.......");
		} 
	}
	 
		/**
		 * 获取所有可执行任务
		 * @return
		 */
		public  List<SchedulerJob> getListEntitys(){
			String column="sch_job as schJob,shc_task as shcTask,sch_name as schName,sch_group as schGroup,sch_status as schStatus,";
			column+="sch_cronExpression as schCronExpression,sch_description as schDescription,sch_create as schCreate,sch_classname as schClassname";
			List<Map<String,Object>> mapList=jdbcTemplate.queryForList("select "+column+" from scheduler_job c where c.sch_status=1");
			List<SchedulerJob> crowdList=new ArrayList<>();
			for (Map<String, Object> map : mapList) {
				 
					SchedulerJob crwodJob;
					try {
						//JSON.toJavaObject(new JSONObject(map), SchedulerJob.class); fastjson转换方式
						crwodJob = (SchedulerJob)BeanUtils.convertMap(SchedulerJob.class, map);
						crowdList.add(crwodJob);
					} catch (IllegalAccessException | InstantiationException | InvocationTargetException
							| IntrospectionException e) {
 						e.printStackTrace();
					}
			}
			return crowdList;
		}
		

}
